﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

namespace com.globalwave.website
{
    public partial class BulletinPage : Common.BasePage
    {
        public BulletinPage()
            : base()
        {
            entityName = "Bulletin";
        }


        public void list()
        {

            NameValueCollection criterion = getCriterion();

            DataSet ds = BulletinService.getInstance().query(criterion, pageIndex, pageSize, null);

            renderListXML(ds);
        }

        public void get()
        {

            NameValueCollection criterion = getCriterion();

            DataSet ds = BulletinService.getInstance().get(criterion);

            renderXML(ds);
        }

        public void create()
        {
            NameValueCollection bulletin = getBulletin();

            int bulletin_id =
                BulletinService.getInstance().create(bulletin);

            bulletin.Set("bulletin_id", bulletin_id.ToString());

            renderSuccessCreateMessage(bulletin);

        }

        public void update()
        {

            NameValueCollection bulletin = getBulletin();

            BulletinService.getInstance().update(bulletin);


            renderSuccessUpdateMessage(bulletin);

        }

        public void delete()
        {
            string[] bulletin_ids = this.getBulletin_ids();

            if (bulletin_ids == null)
            {
                BulletinService.getInstance().delete(getBulletin());
            }
            else
            {
                BulletinService.getInstance().deleteAll(bulletin_ids);
            }

            renderSuccessDeleteMessage();

        }

        /*
        public void possessedByUser()
        {
            DataSet ds = BulletinService.getInstance().possessedByUser(Request.Params.Get("user_id"));

            renderXML(ds);


        }

        public void possessedByOrganization()
        {
            DataSet ds = BulletinService.getInstance().possessedByOrganization(Request.Params.Get("organization_id"));

            renderListXML(ds);

        }
        */

        private NameValueCollection getCriterion()
        {

            return U.filter(Request.Params, "bulletinCriterion")[0];
        }

        private NameValueCollection getBulletin()
        {

            NameValueCollection bulletin = U.filter(Request.Params, "bulletin")[0];
            if ("".Equals(bulletin.Get("pro_bulletin_id")))
            {
                bulletin.Set("pro_bulletin_id", null);
            }

            if ("".Equals(bulletin.Get("board_id")))
            {
                bulletin.Set("board_id", null);
            }

            if ("".Equals(bulletin.Get("topic_id")))
            {
                bulletin.Set("topic_id", null);
            }

            bulletin.Set("content_", U.unescapedParam(bulletin["content_"])) ;
            return bulletin;
        }

        private string[] getBulletin_ids()
        {
            return Request.Params.GetValues("bulletin_ids");
        }
    }
}
